package com.eco.fanliapp.job.historypricejob;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eco.fanliapp.entity.*;
import com.eco.fanliapp.service.*;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 获取商品最低价 标签
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class HistoryLowPriceLabelTask implements Job, Serializable {
    private static final Logger log = LoggerFactory.getLogger(HistoryLowPriceLabelTask.class);


    @Autowired
    private IAppGoodsService iAppGoodsService;
    @Autowired
    private ISyGoodsHistoryPricesService iSyGoodsHistoryPricesService;
    @Autowired
    private ISyLabelGoodsService iSyLabelGoodsService;
    @Autowired
    private ISyLabelTypeService iSyLabelTypeService;

    public HistoryLowPriceLabelTask() {
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap = jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        long beginTime = System.currentTimeMillis();
        log.info("***************************************开始拉取商品历史最低价定时任务-HistoryLowPriceLabelTask：executeCount=" + executeCount + "--beginTime=" + beginTime);

        try {
            Page<AppGoods> page = new Page<>(1, 10000);
            QueryWrapper<AppGoods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("item_top", 1).eq("recommend",1).orderByDesc("update_time");
            IPage<AppGoods> appGoodsIPage = iAppGoodsService.page(page, queryWrapper);

            //查詢本地商品
            if (appGoodsIPage != null && appGoodsIPage.getRecords() != null) {
                for (AppGoods appGoods : appGoodsIPage.getRecords()) {
                    QueryWrapper<SyGoodsHistoryPrices> syUserCollectionWrapper = new QueryWrapper<>();
                    syUserCollectionWrapper.eq("enable", 1);
                    syUserCollectionWrapper.eq("deleted", 0);
                    syUserCollectionWrapper.eq("item_id", appGoods.getItemid());
                    syUserCollectionWrapper.orderByAsc("item_price");
                    List<SyGoodsHistoryPrices> historyList = iSyGoodsHistoryPricesService.list(syUserCollectionWrapper);

                    if (historyList.size() > 0 && historyList != null) {

                        //获取历史最低价
                        Double i = appGoods.getItemendprice() - Double.parseDouble(historyList.get(0).getItemPrice());
                        List<SyLabelType> labelType = iSyLabelTypeService.findLabelType("历史最低价");

                        SyLabelGoods syLabelGoods = new SyLabelGoods();
                        syLabelGoods.setItemId(appGoods.getItemid());
                        syLabelGoods.setLabelTypeId(labelType.get(0).getTableId().toString());
                        syLabelGoods.setLabelCode(labelType.get(0).getLabelCode());
                        syLabelGoods.setCreateTime(new Date());
                        syLabelGoods.setUpdateTime(new Date());
                        syLabelGoods.setEnable(1);
                        if (labelType.size() > 0 && i > 0d) {
                            //删除商品最低价标签
                            syLabelGoods.setDeleted(1);
                            iSyLabelGoodsService.addSyLabelGoods(syLabelGoods);
                            log.info("删除商品最低价标签 ：", syLabelGoods.getItemId());
                        }
                        if (labelType.size() > 0 && i <= 0d) {
                            //添加商品最低价标签
                            syLabelGoods.setDeleted(0);
                            iSyLabelGoodsService.addSyLabelGoods(syLabelGoods);
                            log.info("添加商品最低价标签 ：", syLabelGoods.getItemId());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取商品历史最低价错误");
        }
        long endTime = System.currentTimeMillis();
        log.info("***************************************结束拉取商品历史最低价定时任务-HistoryLowPriceLabelTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - beginTime));

    }
}
